﻿/**
 * $RCSfile: ResponseUtils.java,v $
 * $Revision: 1.1 $
 * $Date: 2009/06/15 08:23:32 $
 *
 * Copyright (C) 2003 ICSS, Inc. All rights reserved.
 *
 * This software is the proprietary information of ICSS, Inc.
 * Use is subject to license terms.
 */
package net.sapfree.display.tags.util;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;

import org.apache.log4j.Logger;

 

/**
 * <p>
 * Title: 信息发布系统（CMS）
 * </p>
 * <p>
 * Description: General purpose utility methods related to generating a servlet
 * response in the Struts controller framework.
 * </p>
 * <p>
 * Copyright: Copyright (c) 2003
 * </p>
 * <p>
 * Company: 中软远东国际
 * </p>
 * 
 * @author Andy
 * @version 1.0
 */
public class ResponseUtils {

	// ------------------------------------------------------- Static Variables

	/**
	 * Commons Logging instance.
	 */
	protected static Logger log = Logger.getLogger(ResponseUtils.class.getClass());

	// --------------------------------------------------------- Public Methods

	/**
	 * Filter the specified string for characters that are senstive to HTML
	 * interpreters, returning the string with these characters replaced by the
	 * corresponding character entities.
	 * 
	 * @param value
	 *            The string to be filtered and returned
	 */
	public static String filter(String value) {

		if (value == null) {
			return null;
		}

		char content[] = new char[value.length()];
		value.getChars(0, value.length(), content, 0);
		StringBuffer result = new StringBuffer(content.length + 50);
		for (int i = 0; i < content.length; i++) {
			switch (content[i]) {
			case '<':
				result.append("&lt;");
				break;
			case '>':
				result.append("&gt;");
				break;
			case '&':
				result.append("&amp;");
				break;
			case '"':
				result.append("&quot;");
				break;
			case '\'':
				result.append("&#39;");
				break;
			default:
				result.append(content[i]);
			}
		}
		return result.toString();

	}

	/**
	 * Write the specified text as the response to the writer associated with
	 * this page. <strong>WARNING</strong> - If you are writing body content
	 * from the <code>doAfterBody()</code> method of a custom tag class that
	 * implements <code>BodyTag</code>, you should be calling
	 * <code>writePrevious()</code> instead.
	 * 
	 * @param pageContext
	 *            The PageContext object for this page
	 * @param text
	 *            The text to be written
	 * 
	 * @exception JspException
	 *                if an input/output error occurs (already saved)
	 */
	public static void write(PageContext pageContext, String text)
			throws JspException {

		JspWriter writer = pageContext.getOut();
		try {
			writer.print(text);
		} catch (IOException e) {
			log.error("", e);
			throw new JspException("write.io"+e.toString());
		}
	}
}